package com.meichis.yndms.ui.activity;


import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.meichis.yndms.R;
import com.meichis.yndms.adapter.MonthBusinessAmountAdapter;
import com.meichis.yndms.databinding.ActivityMonthBusinessAmountBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.MonthBusinessAmountVm;

import java.util.ArrayList;

public class MonthBusinessAmountActivity extends BaseActivity {

    private ActivityMonthBusinessAmountBinding binding;
    private MonthBusinessAmountVm vm;
    private MonthBusinessAmountAdapter adapter;
    private int tag;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_month_business_amount);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MonthBusinessAmountVm.class);
        binding.setVm(vm);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void beforeInit() {
        tag = getIntent().getIntExtra("Tag", 0);
    }

    @Override
    protected void init() {
        if (tag == 1) {
            binding.navigation.txtTitle.setText("本月进货额");
            binding.tvText.setText("进货额（万元）");
        } else {
            binding.navigation.txtTitle.setText("本月销售额");
            binding.tvText.setText("销售额（元）");
        }
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        addObserver();

        adapter = new MonthBusinessAmountAdapter(this, R.layout.item_month_business_amount,tag, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);
        if (tag == 1) {
            vm.getMonthPurchaseAmount();
        } else {
            vm.getMonthSaleAmount();
        }
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getPurchaseAmount().observe(this, list -> {
            binding.rc.setItemViewCacheSize(list.size());
            adapter.setList(list);
        });

        vm.getSaleAmount().observe(this, list -> {
            binding.rc.setItemViewCacheSize(list.size());
            adapter.setList(list);
        });

    }
}
